import type { 
  Tenant, 
  TenantUser, 
  TenantLoginRequest, 
  TenantLoginResponse,
  PublicTenant 
} from '../../../types/tenant';

// Mock租户数据
const mockTenants: Tenant[] = [
  {
    id: 'tenant-demo',
    code: 'demo',
    name: '演示租户',
    domain: 'demo.v3cloud.com',
    description: '用于演示的租户',
    industry: '科技',
    scale: 'startup',
    config: {
      features: {
        multiLanguage: true,
        customTheme: true,
        advancedReports: false,
        apiAccess: true,
        sso: false,
        audit: true,
      },
      security: {
        passwordPolicy: {
          minLength: 8,
          requireUppercase: true,
          requireLowercase: true,
          requireNumbers: true,
          requireSpecialChars: false,
        },
        sessionTimeout: 30,
        maxLoginAttempts: 5,
        requireMFA: false,
      },
      ui: {
        theme: 'light',
        primaryColor: '#1890ff',
      },
      business: {
        timezone: 'Asia/Shanghai',
        dateFormat: 'YYYY-MM-DD',
        numberFormat: '#,##0.00',
        currency: 'CNY',
        language: 'zh-CN',
      },
    },
    subscription: {
      plan: 'basic',
      startDate: '2024-01-01',
      autoRenewal: true,
      billingCycle: 'monthly',
      price: 199,
      currency: 'CNY',
      status: 'active',
    },
    status: 'active',
    contact: {
      adminName: '管理员',
      adminEmail: 'admin@demo.com',
      adminPhone: '13800138000',
    },
    quota: {
      maxUsers: 100,
      maxDepartments: 20,
      maxRoles: 50,
      maxTeams: 30,
      storageLimit: 10,
      apiCallsPerDay: 10000,
      concurrentSessions: 50,
      currentUsers: 25,
      currentDepartments: 5,
      currentRoles: 8,
      currentTeams: 3,
      currentStorage: 2.5,
      todayApiCalls: 1250,
      activeSessions: 8,
    },
    createTime: '2024-01-01T00:00:00Z',
    updateTime: '2024-01-01T00:00:00Z',
    createdBy: 'system',
  },
];

// Mock用户数据
const mockUsers: TenantUser[] = [
  {
    id: 'user-demo-001',
    tenantId: 'tenant-demo',
    username: 'admin',
    email: 'admin@demo.com',
    realName: '管理员',
    roles: ['admin'],
    teamIds: [],
    isSuperAdmin: false,
    status: 'active',
    createTime: '2024-01-01T00:00:00Z',
    updateTime: '2024-01-01T00:00:00Z',
  },
];

export const mockTenantApi = {
  /**
   * 租户登录
   */
  login: async (data: TenantLoginRequest): Promise<TenantLoginResponse> => {
    console.log('Mock 租户登录:', data);
    await new Promise(resolve => setTimeout(resolve, 1000));
    
    // 简单的验证逻辑
    if (data.username === 'admin' && data.password === '123456') {
      const tenant = mockTenants.find(t => t.code === data.tenantCode);
      const user = mockUsers.find(u => u.username === data.username);
      
      if (!tenant) {
        throw new Error('租户不存在');
      }
      
      if (!user) {
        throw new Error('用户不存在');
      }
      
      return {
        token: 'mock-token-' + Date.now(),
        user,
        tenant,
        accessibleTenants: [tenant],
      };
    }
    
    throw new Error('用户名或密码错误');
  },

  /**
   * 获取公开租户信息
   */
  getTenantByCode: async (code: string): Promise<PublicTenant | null> => {
    console.log('Mock 获取租户信息:', code);
    await new Promise(resolve => setTimeout(resolve, 300));
    
    const tenant = mockTenants.find(t => t.code === code);
    if (!tenant) {
      return null;
    }
    
    return {
      id: tenant.id,
      code: tenant.code,
      name: tenant.name,
      logo: tenant.config.ui.logo,
      domain: tenant.domain,
      status: tenant.status,
    };
  },

  /**
   * 搜索公开租户
   */
  searchPublicTenants: async (keyword: string): Promise<PublicTenant[]> => {
    console.log('Mock 搜索租户:', keyword);
    await new Promise(resolve => setTimeout(resolve, 500));
    
    const results = mockTenants
      .filter(tenant => 
        tenant.name.includes(keyword) || 
        tenant.code.includes(keyword)
      )
      .map(tenant => ({
        id: tenant.id,
        code: tenant.code,
        name: tenant.name,
        logo: tenant.config.ui.logo,
        domain: tenant.domain,
        status: tenant.status,
      }));
    
    return results;
  },
};

export default mockTenantApi; 